//
//  ViewController.m
//  UITableView通讯录HomeWork
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CellOfPhone.h"
#import "Person.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray<Person *> *array;
@end

@implementation ViewController

- (void)viewDidLoad {
      [super viewDidLoad];
self.title = @"通讯录";
      _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
      [self.view addSubview:_tableView];
      _tableView.delegate = self;
      _tableView.dataSource = self;
      [_tableView registerClass:[CellOfPhone class] forCellReuseIdentifier:@"resue"];
      [self addressbooklist];
      UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleAdd)];
      self.navigationItem.rightBarButtonItem = button;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
      [super setEditing:editing animated:animated];
      [_tableView setEditing:editing animated:animated];
}
- (void)handleAdd {
      Person *per = [[_array firstObject] copy];
      [_array insertObject:per atIndex:0];
      [_tableView reloadData];

}
- (void)addressbooklist {
      self.array = [NSMutableArray array];
      for (int i = 0; i < 6; i++) {
            Person *per = [[Person alloc]initWithPersonName:[NSString stringWithFormat:@"%c%c", 'A' + i, 'A' + i] Icon:[NSString stringWithFormat:@"%d", i + 1] phoneNumber:[NSString stringWithFormat:@"%d", arc4random() % 10086 + 10086] address:[NSString stringWithFormat:@"%d区%1d委", arc4random() % 10 + 1, arc4random() % 500]];
            [_array addObject:per];
      }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      SecondViewController *second = [[SecondViewController alloc]init];
      second.per = _array[indexPath.row];
      [self.navigationController pushViewController:second animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      return _array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return 160;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      CellOfPhone *cell = [tableView dequeueReusableCellWithIdentifier:@"resue" forIndexPath:indexPath];
      cell.person = _array[indexPath.row];
     
      
      return cell;
}

- (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
}


@end
